package com.crm.controller.permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.exception.ServiceException;
import com.crm.pojo.Role;
import com.crm.service.permission.RoleService;
import com.crm.vo.Result;

@Controller
public class RoleController {

	@Autowired
	private RoleService roleService;

	/**
	 * 查询所有存在的用户
	 * 
	 * @return
	 */
	@PostMapping("query-All-Role")
	@ResponseBody
	public List<Role> queryAllRole() {
		return roleService.queryAlls();
	}

	/**
	 * 分页查询所有角色
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("query-Page-Role-All")
	@ResponseBody
	public Map queryPageRoleAll(int page, int rows) {
		List<Role> list = roleService.queryRoleAllPage(page, rows);
		Long total = roleService.queryRoleCount();
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 根据角色名称模糊查询角色
	 * 
	 * @param rname
	 * @return
	 */
	@PostMapping("query-Roles-ByName")
	@ResponseBody
	public Map queryRolesByName(String text, int page, int rows) {
		List<Role> list = roleService.queryRolesName(text, page, rows);
		Long total = roleService.queryRoleNameCount(text);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 添加角色
	 * 
	 * @param rname
	 */
	@PostMapping("add-One-Role")
	@ResponseBody
	public Result addOneRole(String text) {
		try {
			
			Role role = roleService.queryNameOne(text);
			if (null == role) {
				roleService.addRole(text);
				return Result.SUCCESS;
			} else {
				return new Result("添加失败，角色名重复！");
			}

		} catch (ServiceException e) {
			e.printStackTrace();
			return new Result("添加失败，角色名重复！");
		}
	}

	/**
	 * 删除角色
	 * 
	 * @param rId
	 */
	@PostMapping("delete-One-Role")
	@ResponseBody
	public Result deleteOneRole(Long id) {
		try {
			String name = roleService.queryRoleOneName(id);
			if(name.equals("超级管理员")){
				return new Result("超级管理员不能被删除");
			}else{
				roleService.deleteRole(id);
				return Result.SUCCESS;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(e);
		}
	}

	/**
	 * 根据角色ID查询角色
	 * 
	 * @param rId
	 * @return
	 */
	@GetMapping("query-One-Role")
	@ResponseBody
	public Role queryOneRole(Long id) {
		return roleService.queryRole(id);
	}

	/**
	 * 修改角色信息
	 * 
	 * @param rId
	 * @param rname
	 * @param rstatus
	 */
	@PostMapping("update-One-Role")
	@ResponseBody
	public Result updateOneRole(Long id, String text) {
		try {
			Role r = roleService.queryRole(id);
			Role role = roleService.queryNameOne(text);
			if(r.getRname().equals(text)){
				roleService.updateRole(id, text);
				return Result.SUCCESS;
			}else if (null == role) {
				roleService.updateRole(id, text);
				return Result.SUCCESS;
			} else {
				return new Result("修改失败，角色名重复！");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return new Result("修改失败，角色名重复！");
		}
	}

	@GetMapping("roletable")
	public String toRoleTable() {
		return "permission/role-table";
	}

	@GetMapping("role-add")
	public String toRoleAdd() {
		return "permission/role-add";
	}

	@GetMapping("role-update")
	public String toRoleUpdate() {
		return "permission/role-update";
	}

	@GetMapping("role-to-user")
	public String toRoleToUser() {
		return "permission/role-to-user";
	}
}
